package com.tangkun.environment.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.tangkun.environment.R;
import com.tangkun.environment.activity.QiYeXinXiActivity;
import com.tangkun.environment.entity.gongzuo.InspectViewReturn;
import com.tangkun.environment.utils.LogUtils;
import com.tangkun.environment.view.MyListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author tangkun
 * @ClassName: XiTongTongZhiAdapter
 * @Description: 提交巡查数据列表适配器
 * @date 2016-08-20 上午11:57:36
 */
public class TiJiaoXunChaShuJuAdapter extends BaseAdapter {

    private Context context;
    private List<InspectViewReturn.DataBean.TemplatelistBean> dataList = new ArrayList<InspectViewReturn.DataBean.TemplatelistBean>();

    /**
     * id
     */
    private String id = "";

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public TiJiaoXunChaShuJuAdapter(Context context, List<InspectViewReturn.DataBean.TemplatelistBean> dataList) {
        this.context = context;
        this.dataList = dataList;
    }

    @Override
    public int getCount() {
        return dataList.size() + 1;
    }

    @Override
    public Object getItem(int i) {
//        if (i == 0) {
//            return null;
//        } else {
//            return dataList.get(i - 1);
//        }
        return dataList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_tijiaoxunchashuju, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (dataList != null) {
            if (position == 0) {
                holder.ivTijiaoshujuIcon.setImageResource(R.mipmap.ic_shenhe_huanjingbaohujichuziliao);
                holder.tvTijiaoshujuName.setText("企业信息");
                holder.llTijiaoshujuMore.setVisibility(View.GONE);
                holder.ivTijiaoshujuMore.setImageResource(R.mipmap.ic_down);
                //跳转编辑资料页面
                holder.ivTijiaoshujuMore.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(context, QiYeXinXiActivity.class);
                        intent.putExtra("id", id);//将ID传递过去
                        context.startActivity(intent);
                    }
                });
            } else {
                //图标
                switch (dataList.get(position - 1).ord) {
                    case 4://环境保护基础资料
                        holder.ivTijiaoshujuIcon.setImageResource(R.mipmap.ic_shenhe_huanjingbaohujichuziliao);
                        break;
                    case 5://废水
                        holder.ivTijiaoshujuIcon.setImageResource(R.mipmap.ic_shenhe_feishui);
                        break;
                    case 6://废气
                        holder.ivTijiaoshujuIcon.setImageResource(R.mipmap.ic_shenhe_feiqi);
                        break;
                    case 90://危废
                        holder.ivTijiaoshujuIcon.setImageResource(R.mipmap.ic_shenhe_weifei);
                        break;
                    case 98://噪音
                        holder.ivTijiaoshujuIcon.setImageResource(R.mipmap.ic_shenhe_zaoyin);
                        break;
                    default:
                        holder.ivTijiaoshujuIcon.setImageResource(R.mipmap.ic_shenhe_huanjingbaohujichuziliao);
                        break;
                }

                //标题
                if (!TextUtils.isEmpty(dataList.get(position - 1).module)) {
                    holder.tvTijiaoshujuName.setText(dataList.get(position - 1).module);
                } else {
                    holder.tvTijiaoshujuName.setText("");
                }

                //更多显示与隐藏
                if (dataList.get(position - 1).isShow) {
                    holder.llTijiaoshujuMore.setVisibility(View.VISIBLE);
                } else {
                    holder.llTijiaoshujuMore.setVisibility(View.GONE);
                }

                if (dataList.get(position - 1).itemlist.size() <= 0) {
                    holder.llTijiaoshujuMiddle.setVisibility(View.GONE);
                } else {
                    holder.llTijiaoshujuMiddle.setVisibility(View.VISIBLE);
                }
                //显示与隐藏
                holder.ivTijiaoshujuMore.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (holder.llTijiaoshujuMore.getVisibility() == View.VISIBLE) {
                            holder.llTijiaoshujuMore.setVisibility(View.GONE);
                            holder.ivTijiaoshujuMore.setImageResource(R.mipmap.ic_down);
                            dataList.get(position - 1).isShow = false;
                        } else {
                            holder.llTijiaoshujuMore.setVisibility(View.VISIBLE);
                            dataList.get(position - 1).isShow = true;
                            if (dataList.get(position - 1).itemlist.size() <= 0) {
                                holder.llTijiaoshujuMiddle.setVisibility(View.GONE);
                                holder.mylistview.setVisibility(View.GONE);
                            } else {
                                holder.llTijiaoshujuMiddle.setVisibility(View.VISIBLE);
                                holder.mylistview.setVisibility(View.VISIBLE);
                            }
                            holder.ivTijiaoshujuMore.setImageResource(R.mipmap.ic_up);
                        }
                    }
                });

                //子列表
                TiJiaoXunChaShuJuItemAdapter adapter = new TiJiaoXunChaShuJuItemAdapter(context, dataList.get(position - 1).itemlist);
                adapter.setMyClickListener(new TiJiaoXunChaShuJuItemAdapter.MyClickListener() {
                    @Override
                    public void onMyClick(String type, String ids, String value) {
                        myClickListener.onMyClick(type, ids, value);
                    }
                });
                holder.mylistview.setAdapter(adapter);
            }
        }
        return convertView;

    }

    /**
     * 单条刷新方法实践及原理解析
     *
     * @param listView 这个listview控件
     * @param ids      这条数据bean对应的ids
     */
    public void updateSingleRow(ListView listView, String ids) {
        if (listView != null) {
            int start = listView.getFirstVisiblePosition();
            LogUtils.e("start:" + start + "  last:" + listView.getLastVisiblePosition());
            for (int i = start, j = listView.getLastVisiblePosition(); i < j; i++)
                if (i != 0 && ids.equals(((InspectViewReturn.DataBean.TemplatelistBean) listView.getItemAtPosition(i)).ids)) {
                    View view = listView.getChildAt(i - start);
                    getView(i, view, listView);
                    break;
                }
        }
    }

    /**
     * 自定义接口回调
     */
    private MyClickListener myClickListener;

    public void setMyClickListener(MyClickListener myClickListener) {
        this.myClickListener = myClickListener;
    }

    public interface MyClickListener {
        void onMyClick(String type, String ids, String value);
    }

    static class ViewHolder {
        @BindView(R.id.iv_tijiaoshuju_icon)
        ImageView ivTijiaoshujuIcon;
        @BindView(R.id.tv_tijiaoshuju_name)
        TextView tvTijiaoshujuName;
        @BindView(R.id.iv_tijiaoshuju_more)
        ImageView ivTijiaoshujuMore;
        @BindView(R.id.mylistview)
        MyListView mylistview;
        @BindView(R.id.ll_tijiaoshuju_more)
        LinearLayout llTijiaoshujuMore;
        @BindView(R.id.ll_tijiaoshuju_middle)
        LinearLayout llTijiaoshujuMiddle;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
